﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace game
{
    public class PoolList<T>
    {
        /// <summary>
        /// Active elements
        /// </summary>
        public SafeARList<T> List { get; protected set; }

        protected Pool<T> pool;

        public int FreeElementsCount { get { return pool.Count; } }

        public T Next
        {
            get
            {
                if (!nextAcquired)
                {
                    next = pool.Get();
                    nextAcquired = true;
                }
                return next;
            }
        }
        protected T next;
        protected bool nextAcquired = false;

        public PoolList()
        {
            List = new SafeARList<T>();
            pool = new Pool<T>();
        }

        public void Put(T element)
        {
            pool.Put(element);
        }

        public void ActivateNext()
        {
            if (Next != null)
                List.AddDirectly(next);
            nextAcquired = false;
        }

        public void Resolve()
        {
            foreach (T element in List)
            {
                //if (!element.IsAlive())
                {
                    List.Remove(element);
                    pool.Put(element);
                }
            }
            List.Resolve();
        }

    }
}
